/* 
Scenic Graphics Library
Copyright (C) 2007 Jouni Tulkki

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USApackage scenic;
*/

#include "common.h"
#include "PlainPolygonRenderer.h"


using namespace Scenic::Renderer;


class PlainPolygonPrimitives : public Primitives
{
public:
	PlainPolygonPrimitives();

	void render();
};


PlainPolygonPrimitives::PlainPolygonPrimitives()
{
	setVertexType(VSF_TEX1);
}


void PlainPolygonPrimitives::render()
{
	Context *context = getContext();

	context->device->setBlendFunc(BLEND_ONE, BLEND_ONE_MINUS_SRC_ALPHA);
	getVertexStore()->draw();
}


PlainPolygonPrimitives *plainPolygonPrimitives = new PlainPolygonPrimitives();;

PlainPolygonRenderer::PlainPolygonRenderer(Context *context)
{
	this->context = context;

	context->setPrimitives(plainPolygonPrimitives);

	if(plainPolygonPrimitives->getVertexStore()->getNumVertices() > 100)
		plainPolygonPrimitives->flush();
	vs = plainPolygonPrimitives->getVertexStore();
}

PlainPolygonRenderer::~PlainPolygonRenderer()
{
//	delete vs;
}

void PlainPolygonRenderer::begin()
{
}

void PlainPolygonRenderer::end()
{
	vs->endBlock();
}

void PlainPolygonRenderer::addVertex(double x, double y)
{
	double tx = x * context->transform.m00 + y * context->transform.m01 + context->transform.dx;
	double ty = x * context->transform.m10 + y * context->transform.m11 + context->transform.dy;

	vs->addVertex(tx, ty, context->color.getRGBA32());
}

void PlainPolygonRenderer::addTriangle(int vertex1, bool edge1, int vertex2, bool edge2, int vertex3, bool edge3)
{
	vs->addTriangle(vertex1, vertex2, vertex3);
}

